/**
 * 校验中国大陆身份证号码
 * @param {string} idCard - 身份证号码
 * @returns {boolean} - 是否有效
 */
function validateIDCard (idCard) {
  // 定义正则表达式
  const reg = /(^\d{15}$)|(^\d{17}(\d|X|x)$)/
  if (!reg.test(idCard)) {
    return false // 格式不正确
  }

  // 校验出生日期
  const birthDate = idCard.substring(6, 14)
  if (!validateDate(birthDate)) {
    return false
  }

  // 校验码校验
  const weightFactors = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
  const checkCodes = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
  let sum = 0
  for (let i = 0; i < 17; i++) {
    sum += parseInt(idCard.charAt(i), 10) * weightFactors[i]
  }
  const mod = sum % 11
  const expectedCheckCode = checkCodes[mod]
  const actualCheckCode = idCard.charAt(17).toUpperCase()
  return expectedCheckCode === actualCheckCode
}

/**
* 校验日期格式 YYYYMMDD 是否合法
* @param {string} dateStr - 日期字符串
* @returns {boolean} - 是否合法
*/
function validateDate (dateStr) {
  const year = parseInt(dateStr.substring(0, 4), 10)
  const month = parseInt(dateStr.substring(4, 6), 10)
  const day = parseInt(dateStr.substring(6, 8), 10)
  if (year < 1900 || year > new Date().getFullYear()) return false
  if (month < 1 || month > 12) return false
  const daysInMonth = [31, (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0 ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  return day >= 1 && day <= daysInMonth[month - 1]
}

/**
 * 校验中国大陆手机号
 * @param {string} phone - 需要校验的手机号
 * @returns {boolean} - 如果手机号合法返回 true，否则返回 false
 */
function validateChineseMobile (phone) {
  // 正则表达式解释：
  // ^            : 开始位置
  // 1            : 以数字1开头
  // [3-9]        : 第二位数字为3-9中的任意一个
  // \d{9}        : 后续9位为数字
  // $            : 结束位置
  const regex = /^1[3-9]\d{9}$/
  return regex.test(phone)
}
// 宝宝管理
export const manageColumns = [
  {
    name: '宝宝姓名',
    prop: 'name'
  },
  {
    name: '入学时间',
    prop: 'createTime'
  },
  {
    name: '所在班级',
    prop: 'clssesName'
  },
  {
    name: '出生日期',
    slotName: 'birthday'
  },
  {
    name: '宝宝月龄',
    prop: 'month'
  },
  {
    name: '就学状态',
    prop: 'status',
    slotName: 'status'
  },
  {
    name: '操作',
    slotName: 'operate'
  }
]

// 备忘录
export const memorandumColumns = [
  {
    name: '宝宝姓名',
    prop: 'name'
  },
  {
    name: '所在班级',
    slotName: 'clssesName'
  },
  {
    name: '备忘内容',
    slotName: 'content',
    showOverflowTooltip: true
  },
  {
    name: '图片',
    prop: 'tp',
    slotName: 'img'
  },
  {
    name: '创建时间',
    prop: 'createTime'
  },
  {
    name: '操作',
    slotName: 'operate'
  }
]

//
export const vaccineColumns = [
  {
    name: '宝宝姓名',
    prop: 'name'
  },
  {
    name: '所在班级',
    slotName: 'clssesName'
  },
  {
    name: '疫苗名称',
    slotName: 'type'
  },
  {
    name: '接种状态',
    slotName: 'state'
  },
  {
    name: '接种时间',
    slotName: 'inoculateTime'
  },
  {
    name: '创建时间',
    prop: 'createTime'
  },
  {
    name: '操作',
    slotName: 'operate'
  }
]

const validatorIDCard = (rule, value, callback) => {
  if (value && value.length < 18 || !validateIDCard(value)) {
    callback(new Error('身份证格式错误'))
  } else {
    callback()
  }
}

const validatorMobile = (rule, value, callback) => {
  if (value && value.length < 11 || !validateChineseMobile(value)) {
    callback(new Error('联系电话格式错误'))
  } else {
    callback()
  }
}

export const rules = {
  name: [{ required: true, message: '请输入宝宝姓名', trigger: 'blur' }],
  gender: [{ required: true, message: '请选择宝宝性别', trigger: 'change' }],
  birthday: [{ required: true, message: '请选择出生日期', trigger: 'change' }],
  idCard: [{ required: true, trigger: ['change', 'blur'], validator: validatorIDCard }],
  classesId: [{ required: true, message: '请选择所在班级', trigger: ['change', 'blur'] }],
  content: [{ required: true, message: '请输入备忘内容', trigger: 'blur' }]
}

export const contactRules = {
  name: [{ required: true, message: '请输入家长姓名', trigger: 'blur' }],
  relation: [{ required: true, message: '请选择关系', trigger: ['change', 'blur'] }],
  phone: [{ required: true, message: '请输入联系电话', trigger: 'blur', validator: validatorMobile }]
}

export const vaccineRules = {
  classesId: [{ required: true, message: '请选择所在班级', trigger: ['change', 'blur'] }],
  inoculateTime: [{ required: true, message: '请选择接种时间', trigger: ['change', 'blur'] }],
  type: [{ required: true, message: '请选择疫苗', trigger: ['change', 'blur'] }],
  name: [{ required: true, message: '请选择宝宝', trigger: ['change', 'blur'] }]
}

export const fromSex = [
  {
    value: '1',
    label: '男'
  },
  {
    value: '0',
    label: '女'
  },
  {
    value: '2',
    label: '保密'
  }
]

export const fromNation = [
  {
    label: '汉族'
  },
  {
    label: '壮族'
  },
  {
    label: '满族'
  },
  {
    label: '回族'
  },
  {
    label: '苗族'
  },
  {
    label: '维吾尔族'
  },
  {
    label: '土家族'
  },
  {
    label: '彝族'
  },
  {
    label: '蒙古族'
  },
  {
    label: '藏族'
  },
  {
    label: '布依族'
  },
  {
    label: '侗族'
  },
  {
    label: '瑶族'
  },
  {
    label: '朝鲜族'
  },
  {
    label: '白族'
  },
  {
    label: '哈尼族'
  },
  {
    label: '哈萨克族'
  },
  {
    label: '黎族'
  },
  {
    label: '傣族'
  },
  {
    label: '畲族'
  },
  {
    label: '傈僳族'
  },
  {
    label: '仡佬族'
  },
  {
    label: '东乡族'
  },
  {
    label: '高山族'
  },
  {
    label: '拉祜族'
  },
  {
    label: '水族'
  },
  {
    label: '佤族'
  },
  {
    label: '纳西族'
  },
  {
    label: '羌族'
  },
  {
    label: '土族'
  },
  {
    label: '仫佬族'
  },
  {
    label: '锡伯族'
  },
  {
    label: '柯尔克孜族'
  },
  {
    label: '达斡尔族'
  },
  {
    label: '景颇族'
  },
  {
    label: '毛南族'
  },
  {
    label: '撒拉族'
  },
  {
    label: '布朗族'
  },
  {
    label: '塔吉克族'
  },
  {
    label: '阿昌族'
  },
  {
    label: '普米族'
  },
  {
    label: '鄂温克族'
  },
  {
    label: '怒族'
  },
  {
    label: '京族'
  },
  {
    label: '基诺族'
  },
  {
    label: '德昂族'
  },
  {
    label: '保安族'
  },
  {
    label: '俄罗斯族'
  },
  {
    label: '裕固族'
  },
  {
    label: '乌孜别克族'
  },
  {
    label: '门巴族'
  },
  {
    label: '鄂伦春族'
  },
  {
    label: '独龙族'
  },
  {
    label: '塔塔尔族'
  },
  {
    label: '赫哲族'
  },
  {
    label: '珞巴族'
  }
]

export const fromRelation = [
  { value: '妈妈', label: '妈妈' },
  { value: '爸爸', label: '爸爸' },
  { value: '爷爷', label: '爷爷' },
  { value: '奶奶', label: '奶奶' },
  { value: '外公', label: '外公' },
  { value: '外婆', label: '外婆' },
  { value: '其他', label: '其他' }
]
export const fromEducation = [
  { value: '中专', label: '中专' },
  { value: '大专', label: '大专' },
  { value: '本科', label: '本科' },
  { value: '研究生', label: '研究生' },
  { value: '硕士以上', label: '硕士以上' },
  { value: '其他', label: '其他' }]
export const fromVaccine = [
  { value: '1', label: '乙肝疫苗（第1剂）' },
  { value: '2', label: '卡介苗乙肝疫苗（第2剂）' },
  { value: '3', label: '脊灰疫苗（第1剂）' },
  { value: '4', label: '百白破疫苗（第1剂）' },
  { value: '5', label: '脊灰疫苗（第2剂）' },
  { value: '6', label: '乙肝疫苗（第3剂）' },
  { value: '7', label: 'A群流脑疫苗（第1剂）' },
  { value: '8', label: '麻腮风疫苗（第1剂）' },
  { value: '9', label: '乙脑疫苗（第1剂）' },
  { value: '10', label: '甲肝疫苗（第4剂）' },
  { value: '11', label: '百白破疫苗（第1剂）' },
  { value: '12', label: '乙脑疫苗（第2剂）' },
  { value: '13', label: 'A群C群流脑疫苗（第1剂）' },
  { value: '14', label: '脊灰疫苗（第4剂）' },
  { value: '15', label: '白破疫苗（加强）' },
  { value: '16', label: '群C群流脑疫苗（第1剂）' },
  { value: '17', label: '其他' }]

export const bloodType = [{ value: 'A', label: 'A型血' }, { value: 'B', label: 'B型血' }, { value: 'AB', label: 'AB型血' }, { value: 'O', label: 'O型血' }]
// 宝宝新增/修改
export const babyFrom = [
  {
    type: 'input',
    label: '宝宝姓名',
    value: 'name',
    placeholder: '请输入宝宝姓名'
  },
  {
    type: 'select',
    label: '宝宝性别',
    value: 'gender',
    option: fromSex,
    placeholder: '请选择宝宝性别'
  },
  {
    type: 'picker',
    label: '出生日期',
    value: 'birthday',
    placeholder: '请选择出生日期',
    valueFormat: 'yyyy-MM-dd'
  },
  {
    type: 'inputNumber',
    label: '宝宝月龄',
    value: 'month',
    placeholder: '自动计算',
    disabled: true
  },
  {
    type: 'input',
    label: '身份证号',
    value: 'idCard',
    placeholder: '请输入身份证号',
    max: 18
  },
  {
    type: 'input',
    label: '户口所在地',
    value: 'place',
    placeholder: '请输入户口所在地'
  },
  {
    type: 'select',
    label: '民族',
    value: 'nationality',
    option: fromNation,
    placeholder: '请选择民族'
  },
  {
    type: 'select',
    label: '血型',
    value: 'bloodType',
    option: bloodType,
    placeholder: '请选择血型'
  },
  {
    type: 'input',
    label: '身高',
    value: 'height',
    placeholder: '请输入身高',
    append: 'CM'
  },
  {
    type: 'input',
    label: '体重',
    value: 'weight',
    placeholder: '请输入体重',
    append: 'KG'
  },
  {
    type: 'textarea',
    label: '过敏史',
    value: 'allergy',
    placeholder: '请输入过敏史',
    singleColumn: true
  },
  {
    type: 'textarea',
    label: '疾病史',
    value: 'diseasesHistory',
    placeholder: '请输入疾病史',
    singleColumn: true
  },
  {
    type: 'textarea',
    label: '备注',
    value: 'remark',
    placeholder: '请输入备注',
    singleColumn: true
  }
]

export const contactFrom = [
  {
    type: 'input',
    label: '家长姓名',
    value: 'name',
    placeholder: '请输入家长姓名'
  },
  {
    type: 'select',
    label: '关系',
    option: fromRelation,
    value: 'relation',
    placeholder: '请选择关系'
  },
  {
    type: 'input',
    label: '联系电话',
    value: 'phone',
    placeholder: '请输入联系电话',
    max: 11
  },
  {
    type: 'select',
    label: '学历',
    option: fromEducation,
    value: 'education',
    placeholder: '请选择学历'
  },
  {
    type: 'input',
    label: '工作单位',
    value: 'company',
    placeholder: '请输入工作单位'
  },
  {
    type: 'input',
    label: '职位',
    value: 'position',
    placeholder: '请输入职位'
  },
  {
    type: 'input',
    label: '邮箱',
    value: 'email',
    singleColumn: true,
    placeholder: '请输入邮箱'
  },
  {
    type: 'distpicker',
    label: '家庭住址',
    value: 'jtdz',
    singleColumn: true,
    placeholder: '请选择家庭住址'
  },
  {
    type: 'input',
    label: '具体地址',
    value: 'detailedAddress',
    placeholder: '请输入具体地址'
  }
]
export const memorandumFrom = [
  {
    type: 'select',
    label: '所在班级',
    value: 'classesId',
    placeholder: '请选择所在班级'
  },
  {
    type: 'select',
    label: '宝宝姓名',
    value: 'name',
    placeholder: '请输入宝宝姓名'
  },
  {
    type: 'textarea',
    label: '备忘内容',
    value: 'content',
    placeholder: '请输入备忘内容'
  },
  {
    type: 'upload',
    label: '附件',
    value: 'file',
    placeholder: '请输入备忘内容',
    listType: true
  }
]
export const vaccineFrom = [
  {
    type: 'select',
    label: '所在班级',
    value: 'classesId',
    placeholder: '请选择所在班级'
  },
  {
    type: 'picker',
    label: '接种时间',
    value: 'inoculateTime',
    placeholder: '请选择接种时间',
    pickerOptions: {
      disabledDate (time) {
        return time.getTime() > Date.now()
      }
    }
  },
  {
    type: 'select',
    label: '疫苗名称',
    option: fromVaccine,
    value: 'type',
    placeholder: '请选择疫苗'
  },
  {
    type: 'babyName',
    label: '宝宝姓名',
    value: 'name',
    placeholder: '请选择宝宝',
    loading: false
  },
  {
    type: 'textarea',
    label: '备注',
    value: 'remark',
    placeholder: '请输入备注内容',
    show: true
  }
]
